package com.base.components.common.doc.swagger2;

import com.google.common.collect.Lists;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import java.util.List;

/**
 * DocumentationConfig - 网关路由各服务 api-docs Json 资源配置
 *
 * @author <a href="drakelee1221@gmail.com">LiGeng</a>
 * @version v1.0.0
 * @date 2018-08-24 11:20
 */
public class DocumentationConfig implements SwaggerResourcesProvider {

  private List<ResourceInfo> resources;

  private List<SwaggerResource> SwaggerResources = Lists.newArrayList();

  public DocumentationConfig(List<ResourceInfo> resources) {
    this.resources = resources;
    addSwaggerResource();
  }

  @Override
  public List<SwaggerResource> get() {
    return SwaggerResources;
  }

  private void addSwaggerResource(ResourceInfo resourceInfo) {
    SwaggerResource swaggerResource = new SwaggerResource();
    swaggerResource.setName(resourceInfo.getName());
    swaggerResource.setLocation("/v2/api-docs?group=" + resourceInfo.getName() + "&version=" + resourceInfo.getVersion());
    swaggerResource.setSwaggerVersion(resourceInfo.getVersion());
    SwaggerResources.add(swaggerResource);
  }

  private void addSwaggerResource(){
    if(resources != null){
      for (ResourceInfo resource : resources) {
        addSwaggerResource(resource);
      }
    }
  }
}
